//written by Yevgeniy Bondar

package com.prime.tutorial.moviestore.service.physicalview;

/**
 * Created by IntelliJ IDEA.
 * Users: Admin
 * Date: 28 ��� 2010
 * Time: 14:02:54
 * To change this template use File | Settings | File Templates.
 * @author yevgeniuss
 */


import javax.xml.stream.XMLStreamException;
import java.io.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import javax.xml.stream.*;
import com.prime.tutorial.moviestore.dao.*;
import com.prime.tutorial.moviestore.domain.Device;
import com.prime.tutorial.moviestore.domain.Location;
import com.prime.tutorial.moviestore.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.prime.tutorial.moviestore.service.physicalview.URLReader;

/**
 * Impementation of {@link XmlCreator} <br/>
 * @author yevgeniuss
 */
@Service
public class XmlCreatorImpl implements XmlCreator {

    private DeviceDAO deviceDAO;

    @Autowired
    public XmlCreatorImpl(DeviceDAO deviceDAO) {this.deviceDAO=deviceDAO;}

    public void createTestData() throws IOException, XMLStreamException
    {

        List<Device> devicelist=new ArrayList<Device>();
        devicelist=this.deviceDAO.findAllEnabledNotCPE();

        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        File dataxmlfile=new File("./src/main/webapp/netwplanning/data.xml");

        if (dataxmlfile.exists())
        {
            dataxmlfile.delete();
            dataxmlfile.createNewFile();
        }
        else
        {dataxmlfile.createNewFile();}

        OutputStream out = new FileOutputStream(dataxmlfile);
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);

        Iterator it=devicelist.iterator();



        writer.writeStartDocument();
        writer.writeStartElement("markers");

        Long id=new Long(0);
        Country country=new Country();
        City city=new City();
        Street street=new Street();
        Building building=new Building();

        URLReader googleQuerry=new URLReader();
        String geoanswer = null;

        //for (int i=0; i<5; i++){
        while (it.hasNext()) {
            Device dev=new Device();
            Location loc=new Location();
            dev=(Device)it.next();
            loc=dev.getLocation();
            writer.writeEmptyElement("marker");

            id=loc.getId();
            country=loc.getCountry();
            city=loc.getCity();
            street=loc.getStreet();
            building=loc.getBuilding();


            String address;
            String qaddress;
            String clearStreet=street.getName();

            if (clearStreet.contains(" ")){clearStreet=clearStreet.replace(' ', '+');}
            address = country.getName()+", "+city.getName()+", "+street.getName()+" "+building.getName();
            qaddress= MessageFormat.format("{0}+{1}+{2}+{3}&sensor=false", country.getName(), city.getName(), clearStreet, building.getName());
            String latstr=null;
            String lngstr=null;

            try {

                geoanswer=googleQuerry.geoCode(qaddress);
                //Getting integer latitude and longitude from String geoanswer.

                int startlat, endlat;

                startlat=geoanswer.indexOf("t")+3;
                endlat=startlat+10;
                latstr=geoanswer.substring(startlat, endlat);
                //int lat=parseInt(latstr);

                int startlng, endlng;
                startlng=geoanswer.indexOf("g")+3;
                endlng=startlng+10;
                lngstr=geoanswer.substring(startlng, endlng);
                //int lng=parseInt(lngstr);

                //System.out.println(lat+"       "+lng);
                //String latstr=geoanswer.






            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }


            writer.writeAttribute("id", id.toString());
            //writer.writeAttribute("country", country.getName());
            //writer.writeAttribute("city", city.getName());
            //writer.writeAttribute("street", street.getName());
            //writer.writeAttribute("building",building.getName());

            writer.writeAttribute("address", address);
            writer.writeAttribute("qaddress", qaddress);
            writer.writeAttribute("lat", latstr);
            writer.writeAttribute("lng", lngstr);
            latstr=null;
            lngstr=null;

        }

        /*writer.writeAttribute("lat","50.45110984122117");
        writer.writeAttribute("lng","30.51938514709475");
        writer.writeEmptyElement("marker");
        writer.writeAttribute("name", "router2");
        writer.writeAttribute("lat","50.46488002103265");
        writer.writeAttribute("lng","30.480589675903346");*/
        writer.writeEndElement();
        writer.flush();
        writer.close();

     }
}
